package it.webflora.dao;

import it.webflora.entity.UserName;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserNameDAO {
	
	private PreparedStatement pstm = null;
	private Connection conn = null;
	private ResultSet resultSet = null;
	public UserName userName=new UserName();
	
	
	
	
	public ResultSet resultSet(){
		String query="SELECT * FROM user_name";
		 try {
		 pstm=getPreparedStatement(query);
		resultSet=pstm.executeQuery();
		} catch (SQLException ex) {
		ex.printStackTrace(); }
		return resultSet;
		   }


		public void getInsertSave(UserName userName){
		String query="INSERT INTO user_name VALUES(?,?,?,?)";

		 try {
		 pstm=getPreparedStatement(query);
		 pstm.setInt(1,userName.getId_user());
		 pstm.setString(2,userName.getUser_name());
		 pstm.setInt(3,userName.getCodiceAttivazione());
		 pstm.setInt(4,userName.getId_utente());
		pstm.executeUpdate();

		} catch (SQLException ex) {
		ex.printStackTrace(); }
		finally{
		closeSession();
		}
		   }



		public void saveUpdate(String id){
		String query="UPDATE user_name SET user_name=?,id_utente=? WHERE id_userid_utente='"+id+"'";

		 try {
		 pstm=getPreparedStatement(query);

		
		 pstm.setString(1,userName.getUser_name());
		 pstm.setInt(2,userName.getId_utente());
		pstm.executeUpdate();

		} catch (SQLException ex) {
		ex.printStackTrace(); }
		finally{
		closeSession();
		}
		   }




		public void delete(String id){
		String query="DELETE FROM user_name WHERE id_userid_utente='"+id+"'";
		 try {
		 pstm=getPreparedStatement(query);
		pstm.executeUpdate();

		} catch (SQLException ex) {
		ex.printStackTrace(); }
		finally{
		closeSession();
		}
		   }




		public PreparedStatement getPreparedStatement(String query){
		try {

		Class.forName("com.mysql.jdbc.Driver");
		conn=DriverManager.getConnection("jdbc:mysql://localhost/webflora","root","");
		 pstm=conn.prepareStatement(query);
		 }
		catch (ClassNotFoundException e) {
		e.printStackTrace(); }
		catch (SQLException ex) {
		ex.printStackTrace(); }
		return pstm;}
		 



		public void closeSession(){
		try {
		conn.close();
		pstm.close();
		if(resultSet!=null){
		resultSet.close();
		}
		}catch (SQLException ex) {
		ex.printStackTrace(); }
		}
		
		public String getResultSetMaxId(){
			int idUser=0;
			String stUser=null;
			String query="SELECT max(id_user) FROM user_name";
			 try {
			 pstm=getPreparedStatement(query);
			resultSet=pstm.executeQuery();
			if(resultSet.next())
			{
				stUser=resultSet.getString(1);
			}
			} catch (SQLException ex) {
			ex.printStackTrace(); }
			closeSession();
			return stUser;
			   }
		
		
		public String getResultSetMaxCodAttivazione(){
			int idUser=0;
			String stUser=null;
			String query="SELECT max(codice_attivazione) FROM user_name";
			 try {
			 pstm=getPreparedStatement(query);
			resultSet=pstm.executeQuery();
			if(resultSet.next())
			{
				stUser=resultSet.getString(1);
			}
			} catch (SQLException ex) {
			ex.printStackTrace(); }
			closeSession();
			return stUser;
			   }
		

}
